﻿namespace CRM.DataAccess.DAOs
{
    using System.Collections.Generic;
    using System.Data.Entity;
    using CRM.DataAccess.Entities;
    using System.Linq;

    public class LeadSourceDAO : DbContext
    {
        public LeadSourceDAO()
            : base("name=Entities")
        {            
        }
        public DbSet<LEAD_SOURCE> LEAD_SOURCES { get; set; }
       
        //Get All Lead Source
        public List<LEAD_SOURCE> GetAllLeadSource()
        {
            try
            {
                var Query = from ls in LEAD_SOURCES
                                select ls;
                List<LEAD_SOURCE> contacts = Query.ToList();
                if (contacts.Count > 0)
                    return contacts;
                return null;
            }
            catch
            {
                return null;
            }
        }
        // get Lead Source by name
        public LEAD_SOURCE GetLeadSourceByName(string name)
        {
            try
            {
                var query = from ls in LEAD_SOURCES
                            where ls.LEAD_SOURCE_NAME.ToUpper() == name.ToUpper()
                            select ls;
                List<LEAD_SOURCE> lss = query.ToList();
                if (lss.Count > 0)
                    return lss[0];
                return null;
            }
            catch
            {
                return null;
            }
        }
        //Add new LEad Source
        public int AddNewLeadSource(LEAD_SOURCE ls)
        {
            try
            {
                this.LEAD_SOURCES.Add(ls);
                this.SaveChanges();
                return 1;
            }
            catch
            {
                return -1;
            }
        }
    }
}
